package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sportfitness.module.SprotCourseDescribeModule;
import com.jwthhealth_pub.old.sportfitness.module.SprotCourseInfoModule;
import com.jwthhealth_pub.old.sportfitness.view.adapter.SportcourseSummaryAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/5.
 */
public class SprotCourseInfoSummaryAcitivty extends Activity {
    @BindView(R.id.green)
    View view;
    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.progressbar)
    ProgressBar progressBar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;

    private SprotCourseInfoModule.DataBean dataBean;

    private boolean isRequest;

    public void setRequest(boolean request) {
        isRequest = request;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        setContentView(R.layout.activity_sportcourse_info_summary);
        ButterKnife.bind(this);

        dataBean = (SprotCourseInfoModule.DataBean) getIntent().getExtras().getSerializable(Constant.SPORT_DATA);
        if (!isRequest) {
            Call<SprotCourseDescribeModule> sprotCourseDescribeModuleCall = ApiHelper.sportCourseDescribeRequest(dataBean.getId());
            sprotCourseDescribeModuleCall.enqueue(new Callback<SprotCourseDescribeModule>() {
                @Override
                public void onResponse(Call<SprotCourseDescribeModule> call, Response<SprotCourseDescribeModule> response) {
                    SprotCourseDescribeModule body = response.body();
                    if (body.getCode().equals("0")) {
                        Message message = new Message();
                        message.what = 0;
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constant.SPORT_DATA, body);
                        message.setData(bundle);
                        mHander.sendMessage(message);
                    } else {
                        Log.d("SprotCourseInfoSummaryA", "fail");
                        Message message = new Message();
                        message.what = 1;
                        mHander.sendMessage(message);
                    }
                }

                @Override
                public void onFailure(Call<SprotCourseDescribeModule> call, Throwable t) {
                    Log.d("SprotCourseInfoSummaryA", "error");
                    Message message = new Message();
                    message.what = 1;
                    mHander.sendMessage(message);
                }
            });
        }
        isRequest = true;
        progressbar();
        initTop();
    }

    private void initTop() {
        signTopbar.setTitle("运动健身 " + dataBean.getName());
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private void initRv(SprotCourseDescribeModule body) {
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new SportcourseSummaryAdapter(this, body.getData().getDesc()));
    }

    public void progressbar() {
        if (progressBar.getVisibility() == View.VISIBLE) {
            progressBar.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    public Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressbar();
            isRequest = false;

            if (msg.what == 0) {
                SprotCourseDescribeModule body = (SprotCourseDescribeModule) msg.getData().getSerializable(Constant.SPORT_DATA);
                initRv(body);
            } else {
                Toast.makeText(SprotCourseInfoSummaryAcitivty.this, "网络错误", Toast.LENGTH_SHORT).show();
            }

        }
    };
}
